package  
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import net.GameNet;
	import net.IGameServer;
	import resource.ResourceManager;
	import scene.scene.SceneManager;
	import ui.DialogManager;
	/**
	 * ...
	 * @author carlor
	 */
	public class Engine 
	{
		static private var _instance: Engine;
		private var _main: Sprite;
		private var _stage: Stage;
		private var _sceneLayer		: Sprite;
		private var _uiLayer		: Sprite;
		private var _resizeListener : Function;
		private var _settingPath   	: String;
		private var _resourceManager: ResourceManager;
		private var _sceneManager	: SceneManager;
		private var _dialogManager	: DialogManager;
		private var _gameNet		: GameNet;
		
		public function Engine() 
		{
			_instance = this;
		}
		
		static public function get resourceManager(): ResourceManager
		{
			if (! _instance) return null;
			return _instance._resourceManager;
		}
		
		static public function get sceneManager(): SceneManager
		{
			if (! _instance) return null;
			return _instance._sceneManager;
		}
		
		public function setUp(main: Sprite,  align: String, scaleMode: String, settintPath: String): void
		{
			_main = main;
			_stage = _main.stage != null ? _main.stage : _main.parent.stage;
			_stage.align = align;
			_stage.scaleMode = scaleMode;
			_stage.stageFocusRect = false
			_settingPath = settintPath;
			/*
			_sceneLayer = new Sprite();
			_main.addChild(_sceneLayer);
			_uiLayer = new Sprite();
			_main.addChild(_uiLayer);
			*/
			_resourceManager = new ResourceManager();
			_sceneManager    = new SceneManager();
			_dialogManager   = new DialogManager();
		}
		
		static public function setUpSceneLayer(value: Sprite): void
		{
			if (! _instance) return;
			_instance._sceneLayer = value;
		}
		
		static public function setUpUILayer(value: Sprite): void
		{
			if (! _instance) return;
			_instance._uiLayer = value;
		}
		
		static public function setUpResizeListener(func: Function): void
		{
			if (! _instance) return;
			_instance._resizeListener = func;
			_instance._stage.addEventListener(Event.RESIZE, _instance._resizeListener);
		}
		
		static public function setUpNet(gameServer: IGameServer): void
		{
			if (! _instance) return;
			_instance._gameNet = new GameNet(gameServer);
		}
		
		static public function setUpSceneMsgProc(msgProc: Object): void
		{
			if (! _instance) return;
			_instance._sceneManager.setSceneMsgProc(msgProc);
		}
		
		static public function get settingPath(): String
		{
			if (! _instance) return "";
			return _instance._settingPath;
		}
		
		static public function get mainStage(): Stage
		{
			if (! _instance) return null;
			return _instance._stage;
		}
		
		static public function get main(): Sprite
		{
			if (! _instance) return null;
			return _instance._main;
		}
		
		static public function get sceneLayer(): Sprite
		{
			if (! _instance) return null;
			return _instance._sceneLayer;
		}
		
		static public function get uiLayer(): Sprite
		{
			if (! _instance) return null;
			return _instance._uiLayer;
		}
		
		static public function get gameNet(): GameNet
		{
			if (! _instance) return null;
			return _instance._gameNet;
		}
		
		static public function get dialogManager(): DialogManager
		{
			if (! _instance) return null;
			return _instance._dialogManager;
		}
	}

}